

import 'dart:io';
import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/dialog_sheet/sheet_util.dart';
import 'package:dk_flutter_module_sale/common/plugin/bean/bean_handle_cache.dart';
import 'package:dk_flutter_module_sale/common/plugin/bean/bean_preview_video.dart';
import 'package:dk_flutter_module_sale/common/plugin/bean/bean_record_video.dart';
import 'package:dk_flutter_module_sale/common/plugin/bean/bean_upload_video.dart';
import 'package:dk_flutter_module_sale/common/plugin/plugin_common.dart';
import 'package:dk_flutter_module_sale/common/utils/file_util.dart';
import 'package:dk_flutter_module_sale/sale/api/sale_check_api.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/bean/image_bean.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/bean/password_bean.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/bean/upload_room_paint_bean.dart';
import 'package:dk_flutter_module_sale/sale/sale_check/room_paint/widget/toast_widget.dart';

class RoomPaintLogic {
  //
  static CommonPlugin _commonPlugin = CommonPlugin();
  ///删除本地视频
  static void deleteNativeVideo(List<RecordVideoResultBean> list){
    List<String> deleteList = list.map((RecordVideoResultBean bean){
      return Platform.isIOS ? DKFileManager.instance.documentPath + bean.videoFilePath : bean.videoFilePath;
    }).toList();
    CacheOption cacheOption = CacheOption.asyncMultiRemove(keyList: deleteList);
    _commonPlugin.handleCache(option: cacheOption);
  }

  static String doorTypeStr(AreaBean areaBean,PassWordBean passWordBean){
    if(areaBean.enType == RoomEnvironmentType.RoomEnvironmentTypeInner) {
      return "内门密码：${passWordBean.inPwd}";
    } else {
      return "外门密码：${passWordBean.outPwd}";
    }
  }

  static void previewVideoUri(Url uri){
    PreviewVideoBean previewBean = PreviewVideoBean(
        videoFilePath:uri.videoUrl);
    _commonPlugin.previewVideo(bean: previewBean);
  }

  static void paintVideoTapAction(BuildContext context,AreaBean areaBean,int index,int count,State state){
    if(areaBean.flag) {
      if(objNotEmpty(areaBean.recordVideoResultBean) && index == count - 1) {
        PreviewVideoBean previewBean = PreviewVideoBean(
            videoFilePath:
            areaBean.recordVideoResultBean.videoFilePath);
        _commonPlugin.previewVideo(bean: previewBean);
      } else if(index == count - 1) {
        showActionSheet(context,['录制视频', '从相册选择'],title: "选取视频",callBack:(value) {
          RecordVideoBean bean = new RecordVideoBean(minDuration: 10, maxDuration: 30);
          if(value == 0) {
            _commonPlugin.recordVideo(
                bean: bean,
                handleResult: (RecordVideoResultBean resultBean) {
                  state.setState(() {
                    areaBean.recordVideoResultBean = resultBean;
                  });
                });
          } else {
            _commonPlugin.selectGalleryVideo(
                bean: bean,
                handleResult: (RecordVideoResultBean resultBean) {
                  if(resultBean!= null) {
                    if(int.parse(resultBean.videoFileTime) < 10) {
                      showWidgetToast(ToastWidget(
                        title: "视频时长至少10s",
                      ));
                      return;
                    } else if(int.parse(resultBean.videoFileTime) > 30) {
                      showWidgetToast(ToastWidget(
                        title: "视频时长至多30s",
                      ));
                      return;
                    }
                  }
                  state.setState(() {
                    areaBean.recordVideoResultBean = resultBean;
                  });
                });}
        });
      } else {
        Url uri = areaBean.url[index];
        RoomPaintLogic.previewVideoUri(uri);
      }
    } else {
      Url uri = areaBean.url[index];
      RoomPaintLogic.previewVideoUri(uri);
    }
  }

  static Map createUploadData(UploadVideoResultBean uploadVideoResultBean,
      List<RecordVideoResultBean> sourceList,AreaDataBean dataBean,String suiteId) {
    //匹配房间和对应的视频和图片
    UploadRoomPaintBean saveUploadRoomPaintBean = UploadRoomPaintBean();
    saveUploadRoomPaintBean.suiteId = suiteId;
    List<Videos> successList = uploadVideoResultBean.successList
        .map((UploadVideoSuccessBean resultItem) {
      Videos videos = Videos();
      videos.imageKey = resultItem.imageCode;
      videos.videoKey = resultItem.videoCode;
      for (int i = 0; i < sourceList.length; i++) {
        for(int j = 0;j < dataBean.areas.length;j++) {
          AreaBean areaBean = dataBean.areas[j];
          if (objNotEmpty(areaBean.recordVideoResultBean) && areaBean.recordVideoResultBean.videoFilePath ==
              resultItem.videoPath) {
            videos.areaType = areaBean.areaType;
            videos.timeConsuming =
                int.parse(areaBean.recordVideoResultBean.videoFileTime);
            videos.size = int.parse(areaBean.recordVideoResultBean.videoFileSize);
            videos.roomCode = areaBean.roomCode;
          }
        }
      }
      return videos;
    }).toList();
    saveUploadRoomPaintBean.videos = successList;
    saveUploadRoomPaintBean.suiteAdrr = "";
    Map map = saveUploadRoomPaintBean.toJson();
    return map;
  }
}